var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
import Dialog from '../../../lib/vant-weapp/dialog/dialog';

Page({
  data: {
    orderId: 0,
    orderInfo: {},
    orderGoods: [],
    expressInfo: {},
    flag: false,
    handleOption: {},
    payMethod: false,
    option: [{
        text: '微信支付',
        value: 0
      },
      {
        text: '积分支付',
        value: 1
      },
    ],
    value: 0,
  },
  //支付方式
  payMode({
    detail
  }) {
    this.setData({
      value: detail
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    this.setData({
      orderId: options.id
    });
    this.getOrderDetail();
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getOrderDetail();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  expandDetail: function () {
    let that = this;
    this.setData({
      flag: !that.data.flag
    })
  },
  getOrderDetail: function () {
    wx.showLoading({
      title: '加载中',
    });

    setTimeout(function () {
      wx.hideLoading()
    }, 2000);

    let that = this;
    util.request(api.OrderDetail, {
      orderId: that.data.orderId
    }).then(function (res) {
      if (res.errno === 0) {
        console.log(res.data);
        that.setData({
          orderInfo: res.data.orderInfo,
          orderGoods: res.data.orderGoods,
          handleOption: res.data.orderInfo.handleOption,
          expressInfo: res.data.expressInfo
        });
      }

      wx.hideLoading();
    });
  },
  // “去付款”按钮点击效果
  payOrder: function () {
    let that = this;
    switch (that.data.value) {
      case 0:
        util.request(api.OrderPrepay, {
          orderId: that.data.orderId
        }, 'POST').then(function (res) {
          console.log(res);
          if (res.errno === 0) {
            const payParam = res.data;
            console.log("支付过程开始");
            wx.requestPayment({
              'timeStamp': payParam.timeStamp,
              'nonceStr': payParam.nonceStr,
              'package': payParam.packageValue,
              'signType': payParam.signType,
              'paySign': payParam.paySign,
              'success': function (res) {
                console.log("支付过程成功");
                util.redirect('/pages/ucenter/order/order');
              },
              'fail': function (res) {
                console.log("支付过程失败");
                util.showErrorToast('支付失败');
              },
              'complete': function (res) {
                console.log("支付过程结束")
              }
            });
          } else {
            wx.showToast({
              title: res.errmsg,
              icon: 'none'
            })
          }
        });
        break;
      case 1:
        Dialog.confirm({
            title: '请确认',
            message: '当前使用积分支付',
          })
          .then(() => {
            console.log('积分支付');
            util.request(api.AuthUserInfo).then(res => {
              if (res.data.integral >= this.data.orderInfo.actualPrice) {
                util.request(api.OrderJfpay + '?orderid=' + that.data.orderId, {}, 'post').then(res => {
                  if (res.errno == 0) {
                    wx.redirectTo({
                      url: '/pages/payResult/payResult?status=1&orderId=' + that.data.orderId
                    });
                  } else {
                    wx.showToast({
                      title: res.data,
                      icon: 'none',
                      mask: true
                    })
                  }
                })
              } else {
                util.showErrorToast('积分余额不足');
                this.setData({
                  value1: 0
                })
              }
            })
          })
          .catch(() => {});
        break;
      default:
        break;
    }


  },
  // “取消订单”点击效果
  cancelOrder: function () {
    let that = this;
    let orderInfo = that.data.orderInfo;

    wx.showModal({
      title: '',
      content: '确定要取消此订单？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderCancel, {
            orderId: orderInfo.id
          }, 'POST').then(function (res) {
            if (res.errno === 0) {
              wx.showToast({
                title: '取消订单成功'
              });
              util.redirect('/pages/ucenter/order/order');
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  // “取消订单并退款”点击效果
  refundOrder: function () {
    let that = this;
    let orderInfo = that.data.orderInfo;

    wx.showModal({
      title: '',
      content: '确定要取消此订单？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderRefund, {
            orderId: orderInfo.id
          }, 'POST').then(function (res) {
            if (res.errno === 0) {
              wx.showToast({
                title: '取消订单成功'
              });
              util.redirect('/pages/ucenter/order/order');
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  // “删除”点击效果
  deleteOrder: function () {
    let that = this;
    let orderInfo = that.data.orderInfo;

    wx.showModal({
      title: '',
      content: '确定要删除此订单？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderDelete, {
            orderId: orderInfo.id
          }, 'POST').then(function (res) {
            if (res.errno === 0) {
              wx.showToast({
                title: '删除订单成功'
              });
              util.redirect('/pages/ucenter/order/order');
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  // “确认收货”点击效果
  confirmOrder: function () {
    let that = this;
    let orderInfo = that.data.orderInfo;

    wx.showModal({
      title: '',
      content: '确认收货？',
      success: function (res) {
        if (res.confirm) {
          util.request(api.OrderConfirm, {
            orderId: orderInfo.id
          }, 'POST').then(function (res) {
            if (res.errno === 0) {
              wx.showToast({
                title: '确认收货成功！'
              });
              util.redirect('/pages/ucenter/order/order');
            } else {
              util.showErrorToast(res.errmsg);
            }
          });
        }
      }
    });
  },
  // “申请售后”点击效果
  aftersaleOrder: function () {
    if (this.data.orderInfo.aftersaleStatus === 0) {
      util.redirect('/pages/ucenter/aftersale/aftersale?id=' + this.data.orderId);
    } else {
      util.redirect('/pages/ucenter/aftersaleDetail/aftersaleDetail?id=' + this.data.orderId);
    }
  },
  onReady: function () {
    // 页面渲染完成
    util.request(api.AuthUserInfo).then(res => {
      if (res.errno === 0) {
        if (res.data.integral < this.data.orderInfo.actualPrice) {
          this.setData({
            payMethod: true
          })
        }
      }
    })
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  }
})